<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>使用定位进行文本居中</title>
  <style>
    #d1 {
      width: 150px;
      height: 60px;
      border:1px solid red;
    }
    #d1 span {
      position: relative;
      top:18px;
      left:20px;
    }
  </style>
</head>
<body>
  <div id="d1">
    <span>测试容器</span>
  </div>
</body>
</html>